package ${packageName};

import com.dean.system.dao.${tableInfo.firstUpperEntityName}Dao;
import com.dean.system.entity.${tableInfo.firstUpperEntityName};
import com.dean.system.service.${tableInfo.firstUpperEntityName}Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;
import cn.hutool.core.util.IdUtil;

/**
*
* @Description ${tableInfo.firstUpperEntityName}ServiceImpl
* @Author dean
* @Date 2023-05-26 17:46:10
*/
@Service
public class ${tableInfo.firstUpperEntityName}ServiceImpl implements ${tableInfo.firstUpperEntityName}Service {

    private final ${tableInfo.firstUpperEntityName}Dao dao;
    private final SqlSessionFactory sqlSessionFactory;

    public ${tableInfo.firstUpperEntityName}ServiceImpl(${tableInfo.firstUpperEntityName}Dao dao,SqlSessionFactory sqlSessionFactory) {
        this.dao = dao;
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public int insert(${tableInfo.firstUpperEntityName} entity) {
        entity.setDataId(IdUtil.getSnowflakeNextId());
        return dao.insert(entity);
    }

    @Override
    public int update(${tableInfo.firstUpperEntityName} entity) {
        return dao.update(entity);
    }

    @Override
    public int deleteById(Long dataId) {
        return dao.deleteById(dataId);
    }

    @Override
    public int deleteByIds(Long[] dataIds) {
        return dao.deleteByIds(dataIds);
    }

    @Override
    public ${tableInfo.firstUpperEntityName} selectById(Long dataId) {
        return dao.selectById(dataId);
    }

    @Override
    public List<${tableInfo.firstUpperEntityName}> selectList(${tableInfo.firstUpperEntityName} entity) {
        return  dao.selectList(entity);
    }

    @Override
    public PageInfo<${tableInfo.firstUpperEntityName}> pageList(${tableInfo.firstUpperEntityName} entity, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(dao.selectList(entity));
    }

    @Override
    public int changeStatus(Long dataId, String status){
        return dao.changeStatus(dataId,status);
    }
    @Override
    public int batchInsert(List<${tableInfo.firstUpperEntityName}> entities) {
        try(SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)){
            entities.stream().forEach(entity->{
                entity.setDataId(IdUtil.getSnowflakeNextId());
                this.repository.insert(entity);
            });
            // 清除缓存
            session.clearCache();
            // 提交
            session.commit();
        }
        return entities.size();
    }

}
